﻿using System;
using UnityEngine;
using System.Collections;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
using System.Text;
using JYLib;
using UnityEngine.UI;
using Color = System.Drawing.Color;
using Debug = UnityEngine.Debug;
using Image = System.Drawing.Image;

public class ConnectUI : MonoBehaviour {


    public Button ConnectBtn;
    public Text text;
    public PrintDocument printDocument;
    private System.Drawing.Image im;
    public Text CoText;
    public InputField InputField;
    


    void Awake()
    {

        EventTriggerListener.Get(ConnectBtn.gameObject).onClick = OnClickConnect;

        printDocument = new PrintDocument();
        printDocument.DefaultPageSettings.PaperSize = new PaperSize("6cun",600,400);
        printDocument.PrintPage += new PrintPageEventHandler(this.printDocument_PrintPage);
        //im = Image.FromFile("F:/JCNumber/JCPaihao/Assets/back.jpg");
        Debug.Log("1============="+ Application.persistentDataPath);
        Debug.Log("2=============" + Application.dataPath);
        CoText.text = Application.dataPath;
        //

        text.text = "";




    }

    private void printDocument_PrintPage(object sender, PrintPageEventArgs e)
    {
        Debug.Log("打印了");
        e.Graphics.Clear(Color.FloralWhite);
        e.Graphics.DrawImage(im, new Point(0, 0));
        e.Graphics.DrawString("----票据----",new System.Drawing.Font(new FontFamily("宋体"),22, System.Drawing.FontStyle.Bold),System.Drawing.Brushes.Black,215,100 );
        im.Dispose();


    }


    public void OnCtest(GameObject obj)
    {
        // OnPrint("F:/JCNumber/JCPaihao/Assets/test.xls");
        printDocument.Print();
    }





    void OnPrint(string path)
    {

        Process pro = new Process();
        pro.StartInfo.CreateNoWindow = true;
        pro.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
        pro.StartInfo.UseShellExecute = true;
        pro.StartInfo.FileName = path;
       
        pro.StartInfo.Verb = "print";
        pro.Start();

    }


    void Start()
    {

        TNAutoJoin.instance.OnConnectingAction = OnConnectingAction;
     //   TNAutoJoin.instance.OnDisconnectAction = OnDisconnectAction;
        TNAutoJoin.instance.OnJoinChannelAction = OnJoinChannelAction;
        //
        //读取配置
#if UNITY_STANDALONE_WIN || UNITY_EDITOR

        string path =  Application.dataPath + "/setting.txt";
        FileInfo _fileInfo = new FileInfo(path);
        if (_fileInfo.Exists)
        {
            
            FileStream _fileStream = _fileInfo.OpenRead();
            byte[] info = new byte[_fileStream.Length];
            _fileStream.Read(info, 0, info.Length);
            string str = new UTF8Encoding(true).GetString(info);
            GlobalVO.GetInstance().SettingVo= LitJson.JsonMapper.ToObject<SettingVO>(str);
            Debug.Log(GlobalVO.GetInstance().SettingVo.port+"存在配置文件" + str);
            
        }
        else
        {
            SettingVO setvo = new SettingVO();
            setvo.isuserTestBtn = true;
            setvo.password = "yuangong";
            setvo.user = "yuangong";
            setvo.port = "127.0.0.1";
            FileInfo fileinfo = new FileInfo(path);
            StreamWriter fileStream = fileinfo.CreateText();
            fileStream.WriteLine(LitJson.JsonMapper.ToJson(setvo));
            fileStream.Close();
        }

#endif


        if (GlobalVO.GetInstance().SettingVo!= null)
        {
            InputField.text = GlobalVO.GetInstance().SettingVo.port;
        }
        else
        {
            SettingVO setvo = new SettingVO();
            setvo.isuserTestBtn = true;
            setvo.password = "yuangong";
            setvo.user = "yuangong";
            setvo.port = "127.0.0.1";
            GlobalVO.GetInstance().SettingVo = setvo;
            InputField.text = GlobalVO.GetInstance().SettingVo.port;

        }


    }


    void OnClickConnect(GameObject btn)
    {
        if(!GlobalVO.GetInstance().isConnecting)
        {
            TNAutoJoin.instance.serverAddress = text.text;
            //点击连接
            TNAutoJoin.instance.Connect();
           
        }
        else
        {


        }



    }


    void OnConnectingAction(bool res)
    {
        GlobalVO.GetInstance().isConnecting = res;
    }


    void OnDisconnectAction()
    {

        GlobalVO.GetInstance().isConnecting = false;
    }

    void OnJoinChannelAction()
    {

    }
}

public class SettingVO
{

    public string port;
    public string user;
    public string password;
    public bool isuserTestBtn;

}
